package scau.erps.view.base;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import scau.erps.app.Erps;
import scau.erps.entity.Material;
import scau.erps.service.MaterialService;
import scau.erps.service.impl.MaterialServiceImpl;
import scau.erps.util.CalculateMaterial;
import scau.erps.util.CalculateQuarter;
import scau.erps.util.FormatValidation;

/**
 * 用于申请原材料订单 
 * @author  outsider
 * 
 */
public class MaterialOrderDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JTextField m1TextField,m2TextField,m3TextField,m4TextField;
	private JLabel priceTextField,periodM1M2TextField,periodM3M4TextField;
	private JButton confirmButton,skipButton; 
	private JPanel contentPanel;
	private MaterialService materialService=Erps.getIocInstance().get(MaterialServiceImpl.class);
	private DecisionPanel superPanel;
	public MaterialOrderDialog(DecisionPanel superPanel) {
       this.setTitle("原材料订单");
       this.setSize(400, 260);
       this.superPanel=superPanel;
       this.setModal(true);
	   contentPanel = new JPanel();
	   contentPanel.setLayout(new BorderLayout());
	   contentPanel.add(getMaterialOrderPanel(), BorderLayout.CENTER);
	   this.setContentPane(contentPanel);
	   this.setLocationRelativeTo(null);
	   this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	   this.setVisible(true);
	}
	private JPanel getMaterialOrderPanel(){
		JPanel materialOrderPanel = new JPanel();		
		materialOrderPanel.setLayout(new BorderLayout());		
		JLabel m1Label = new JLabel("M1",JLabel.CENTER);
		JLabel m2Label = new JLabel("M2",JLabel.CENTER);
		JLabel m3Label = new JLabel("M3",JLabel.CENTER);
		JLabel m4Label = new JLabel("M4",JLabel.CENTER);				
		JLabel periodM1M2Label = new JLabel(" M1M2账期：");
		JLabel periodM3M4Label = new JLabel(" M3M4账期：");
		JLabel priceLabel = new JLabel(" 总价：");	
		m1TextField = new JTextField();
		m1TextField.setText("0");
		m1TextField.setColumns(8);	
		m2TextField = new JTextField();
		m2TextField.setText("0");
		m2TextField.setColumns(8);				
		m3TextField = new JTextField();
		m3TextField.setText("0");
		m3TextField.setColumns(8);		
		m4TextField = new JTextField();
		m4TextField.setText("0");
		m4TextField.setColumns(8);			
		periodM1M2TextField = new JLabel();
		periodM1M2TextField.setText("现金");	
		periodM3M4TextField = new JLabel();
		periodM3M4TextField.setText("现金");
		priceTextField = new JLabel();
		priceTextField.setText("0");
		
		//添加文本改变监听器
		Document[] doc=new Document[4];
		doc[0]=m1TextField.getDocument();
		doc[1]=m2TextField.getDocument();
		doc[2]=m3TextField.getDocument();
		doc[3]=m4TextField.getDocument();
		for (int i=0;i<=3;i++){
			doc[i].addDocumentListener(new DocumentListener() {
				public void insertUpdate(DocumentEvent de) {
					documentEventProcess();								
				}
				public void changedUpdate(DocumentEvent de) {					
					changePrice();
					changeperiodM1M2();
					changeperiodM3M4();									
				}				
				public void removeUpdate(DocumentEvent de) {					
					changePrice();
					changeperiodM1M2();
					changeperiodM3M4();								
				}
			});
		}		
		confirmButton = new JButton("确定购买");	
		confirmButton.addActionListener(this);
		skipButton = new JButton("退出");
		skipButton.addActionListener(this);
		JPanel jPanel1 = new JPanel(new GridLayout(2,4,5,5));
		jPanel1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));
		jPanel1.add(m1Label);
		jPanel1.add(m1TextField);
		jPanel1.add(m2Label);
		jPanel1.add(m2TextField);
		jPanel1.add(m3Label);
		jPanel1.add(m3TextField);
		jPanel1.add(m4Label);
		jPanel1.add(m4TextField);
		materialOrderPanel.add(jPanel1,BorderLayout.NORTH);		
		JPanel jPanel2 = new JPanel();
		jPanel2.setBorder(BorderFactory.createTitledBorder("应付款账期优惠"));
		jPanel2.add(periodM1M2Label);
		jPanel2.add(periodM1M2TextField);
		jPanel2.add(periodM3M4Label);
		jPanel2.add(periodM3M4TextField);
		jPanel2.add(priceLabel);		
		jPanel2.add(priceTextField);
		materialOrderPanel.add(jPanel2,BorderLayout.CENTER);		
		JPanel jPanel3 = new JPanel();				
		jPanel3.add(confirmButton);
		jPanel3.add(skipButton);
		materialOrderPanel.add(jPanel3,BorderLayout.SOUTH);
		return materialOrderPanel;
	}
	public void documentEventProcess(){
		if(FormatValidation.isNumeric(m1TextField.getText()) && FormatValidation.isNumeric(m2TextField.getText()) && FormatValidation.isNumeric(m3TextField.getText())
				&& FormatValidation.isNumeric(m4TextField.getText())){
			changePrice();
			changeperiodM1M2();
			changeperiodM3M4();
		}else{
			JOptionPane.showMessageDialog(null, "输入的不是数字", "警告",JOptionPane.WARNING_MESSAGE);
		}
	}
	//监听总价文本框方法	
	private void changePrice(){
		int m1=0,m2=0,m3=0,m4=0;		
		if(m1TextField.getText().equals("")==false) m1=Integer.parseInt(m1TextField.getText());			
	    if(m2TextField.getText().equals("")==false)	m2=Integer.parseInt(m2TextField.getText());			
	    if(m3TextField.getText().equals("")==false)	m3=Integer.parseInt(m3TextField.getText());
	    if(m4TextField.getText().equals("")==false) m4=Integer.parseInt(m4TextField.getText());		
		priceTextField.setText(Integer.toString(m1+m2+m3+m4));
	}
	//监听M1M2账期框方法
	private void changeperiodM1M2(){	
		periodM1M2TextField.setText(CalculateMaterial.getperiod(m1TextField.getText(), m2TextField.getText()));
	}
	//监听M3M4账期框方法
	private void changeperiodM3M4(){
		periodM3M4TextField.setText(CalculateMaterial.getperiod(m3TextField.getText(), m4TextField.getText()));
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==confirmButton){
			int m1=0,m2=0,m3=0,m4=0,tot=0; String message="";
			int team=Erps.getCurrentTeam().getId();
			String beginTime=Erps.getSand().getTime();
			String getTimeM1M2=CalculateQuarter.addOneQuarter(Erps.getSand().getTime());
			String getTimeM3M4=CalculateQuarter.addTwoQuarter(Erps.getSand().getTime());
			String payTimeM1M2=getTimeM1M2;
			String payTimeM3M4=getTimeM3M4;
			try{
				String periodM1M2=(CalculateMaterial.getperiod(m1TextField.getText(), m2TextField.getText()));
			    String periodM3M4=(CalculateMaterial.getperiod(m3TextField.getText(), m4TextField.getText()));
			    if (periodM1M2.equals("现金")==false) payTimeM1M2=CalculateQuarter.calculateQuarter(getTimeM1M2,periodM1M2);
			    if (periodM3M4.equals("现金")==false) payTimeM3M4=CalculateQuarter.calculateQuarter(getTimeM3M4,periodM3M4);			    
				if (m1TextField.getText().equals("")==false) m1=Integer.parseInt(m1TextField.getText());
				if (m2TextField.getText().equals("")==false) m2=Integer.parseInt(m2TextField.getText());
				if (m3TextField.getText().equals("")==false) m3=Integer.parseInt(m3TextField.getText());
				if (m4TextField.getText().equals("")==false) m4=Integer.parseInt(m4TextField.getText());
				if(m1<0||m2<0||m3<0||m4<0)  throw new NumberFormatException();	
				if (m1>0){
				   Material materialOrder=new Material(team, "M1", m1, beginTime, getTimeM1M2, payTimeM1M2, "已申请");
				   materialService.applyMaterialOrder(materialOrder);
				   tot++;
				   message=message+"M1数量： "+m1+" 入库时间 "+getTimeM1M2+" 付款时间 "+payTimeM1M2+"\n";
				}
				if (m2>0){
				   Material materialOrder=new Material(team, "M2", m2, beginTime, getTimeM1M2, payTimeM1M2, "已申请");
				   materialService.applyMaterialOrder(materialOrder);
				   tot++;
				   message=message+"M2数量： "+m2+" 入库时间 "+getTimeM1M2+" 付款时间 "+payTimeM1M2+"\n";
				}
				if (m3>0){
				    Material materialOrder=new Material(team, "M3", m3, beginTime, getTimeM3M4, payTimeM3M4, "已申请");
					materialService.applyMaterialOrder(materialOrder);
					tot++;
					message=message+"M3数量： "+m3+" 入库时间 "+getTimeM3M4+" 付款时间 "+payTimeM3M4+"\n";
				}
				if (m4>0){
				    Material materialOrder=new Material(team, "M4", m4, beginTime, getTimeM3M4, payTimeM3M4, "已申请");
					materialService.applyMaterialOrder(materialOrder);
					tot++;
					message=message+"M4数量： "+m4+" 入库时间 "+getTimeM3M4+" 付款时间 "+payTimeM3M4+"\n";
				}
			    message=message+"本季度共申请 "+tot+" 笔原材料订单"+"\n"+"盘面操作：领取对应数量黄色筹码并摆放于对应位置";
			    JOptionPane.showMessageDialog(null, message, "提示",JOptionPane.INFORMATION_MESSAGE);
				superPanel.getDashBoardPanel().setPayableData();
				superPanel.getDashBoardPanel().setDashBoardData(12);
			    dispose();
		}
			catch(NumberFormatException ne){
				JOptionPane.showMessageDialog(null, "请输入大于零的数字", "提示",JOptionPane.INFORMATION_MESSAGE);
			}
			
		}
		else if(e.getSource()==skipButton){
			dispose();
		}
		
	}
}
